//
//  MeViewController.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "MeViewController.h"
#import "MeCommonJumpCell.h"
#import "MeTableHeaderView.h"
#import "MeFiveImageCell.h"
#import "WineCategoryVC.h"
#import "AdressMangerVC.h"
#import "MyHouseListVC.h"
#import <EaseUILite/EaseSDKHelper.h>

#import "PaymentRecordVC.h"
#import "BaseNavigationController.h"
#import "MyBalanceController.h"
#import "GoodsEvaluateVC.h"

#import "MyAllOrderCount.h"

@interface MeViewController ()
/** <#note#> */
@property (nonatomic, strong) MeTableHeaderView *headerView;

/** <#note#> */
@property (nonatomic, strong) MyAllOrderCount *conutModel;
@end

@implementation MeViewController

- (MeTableHeaderView *)headerView{
    if (!_headerView) {
        
        MeTableHeaderView *temp = (MeTableHeaderView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MeTableHeaderView class]) owner:nil options:nil].firstObject;

        
        if (IPhoneX) {
            
            temp.frame = CGRectMake(0, 0, KScreenW, 200);
            
        } else {
            
            temp.frame = CGRectMake(0, 0, KScreenW, 176);

        }
        
        _headerView = temp;
        
    }
    
    
    
    return _headerView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.groupTableView.tableHeaderView = self.headerView;
    
    [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeCommonJumpCell class]) bundle:nil] forCellReuseIdentifier:[MeCommonJumpCell identifierStr]];
    
     [self.groupTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeFiveImageCell class]) bundle:nil] forCellReuseIdentifier:[MeFiveImageCell identifierStr]];

    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadCountData) name:@"userDidLoginOrInfoChange" object:nil];
    
    
    if (@available(iOS 11.0, *)) {
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
        
    }else{
        
        self.automaticallyAdjustsScrollViewInsets = NO;
        
        self.groupTableView.contentInset = UIEdgeInsetsMake(0, 0, 49, 0);
        
    }

    // 设置标题
//    if (@available(iOS 11.0, *)) {
//
//        self.navigationController.navigationBar.prefersLargeTitles = YES;
//
//        self.navigationItem.largeTitleDisplayMode =  UINavigationItemLargeTitleDisplayModeAlways;
//
//    }
    
    
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    [self loadCountData];
}




#pragma mark - tableviewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return  2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 2;
    }else{
        
        return 7;
        
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            MeCommonJumpCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MeCommonJumpCell identifierStr]];
            
            tableCell.index = indexPath;
            
            return tableCell;
        } else {
            MeFiveImageCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MeFiveImageCell identifierStr]];
            
            tableCell.model = self.conutModel;
            
            return tableCell;
        }

    } else if (indexPath.section == 1){
        MeCommonJumpCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MeCommonJumpCell identifierStr]];
        
        tableCell.index = indexPath;

        return tableCell;
    }else{
        
        MeCommonJumpCell *tableCell = [tableView dequeueReusableCellWithIdentifier:[MeCommonJumpCell identifierStr]];
        
        tableCell.index = indexPath;

        
        return tableCell;
        
    }
    

    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 ) {
        
        if (myViewModel.hideShopping) {
            return  0.00001;
        } else {
            if (indexPath.row == 0) {
                return 46;
            } else {
                return 72;
            }
        }
        
      
        
    } else {
         return 52;
    }
    
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (myViewModel.hideShopping) {
        
        return 0.00001;
    } else {
        
        if (section == 0) {
            
            return 0.00001;
            
        } else if (section == 1) {
            
            return 16;
            
        }else{
            
            return 16;
            
        }
    }
    
    
   

}



- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
    return nil;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *temp = [[UIView alloc]init];
    temp.backgroundColor = viewBgGrey;
    return temp;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 1 && indexPath.row == 6) {
        
        if (myViewModel.userIsLogin) {
             
            
            UIAlertController *contr = [UIAlertController alertControllerWithTitle:nil message:@"确定退出当前账号?" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                 [myViewModel logOut];
                
                
            }];
            
            
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            
            
            [contr addAction:action2];
            [contr addAction:action1];
            
            [self presentViewController:contr animated:YES completion:nil];
            
            
            
            
        } else {
            
            [myViewModel.rootController login];

        }
        
    } else {
        
        if ( !(myViewModel.userIsLogin)) {
            
            
            [myViewModel.rootController login];
            
            return;
        }
    }
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            WineCategoryVC *temp = [[WineCategoryVC alloc]init];
            
            temp.type = @"allOrder";
            
            temp.seletIndex = 0;
            
            [self.navigationController pushViewController:temp animated:YES];
        }
    }else if (indexPath.section == 1){
        
        if (indexPath.row == 0) {
            
            MyBalanceController *temp = [[MyBalanceController alloc]init];
            
            
            [self.navigationController pushViewController:temp animated:YES];

            
        } else if (indexPath.row == 1) {
            
            PaymentRecordVC *temp = [[PaymentRecordVC alloc]init];
            
            temp.type = @"3";

            [self.navigationController pushViewController:temp animated:YES];
            
        }else if (indexPath.row == 2) {
            
            PaymentRecordVC *temp = [[PaymentRecordVC alloc]init];
            
            temp.type = @"1";

            [self.navigationController pushViewController:temp animated:YES];
            
        }else if (indexPath.row == 3) {
            
            
            PaymentRecordVC *temp = [[PaymentRecordVC alloc]init];
            
            temp.type = @"2";
            
            [self.navigationController pushViewController:temp animated:YES];
            
  

            
        }else if (indexPath.row == 4) {
            
            MyHouseListVC *temp = [[MyHouseListVC alloc]init];
            
            [self.navigationController pushViewController:temp animated:YES];
            
            
          
        }else if (indexPath.row == 5) {
            
            
            AdressMangerVC *temp = [[AdressMangerVC alloc]init];
            
            [self.navigationController pushViewController:temp animated:YES];
          
        }else if (indexPath.row == 6) {
            

//            [self fdsfdsf];
//            GoodsEvaluateVC *pictureVC = [[GoodsEvaluateVC alloc]init];
//
//            BaseNavigationController *nav = [[BaseNavigationController alloc]initWithRootViewController:pictureVC];
//
//            nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//
//            [self presentViewController:nav animated:YES completion:nil];

        }
        
        
       
    }
    
   
    
}

/*
 alipay_sdk=alipay-sdk-php-20161101&app_id=2017051807273196&biz_content=%7B%22body%22%3A%22350568742157209002_r%22%2C%22subject%22%3A+%22%E5%B0%8F%E9%B2%B8%E5%95%86%E5%9F%8E%E5%95%86%E5%93%81%E8%AE%A2%E5%8D%95_350568742157209002%22%2C%22out_trade_no%22%3A+%22350568742157209002_r%22%2C%22timeout_express%22%3A+%2230m%22%2C%22total_amount%22%3A+%22140.00%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fmall.xjscvip.com%2Fmobile%2Fapi%2Fpayment%2Falipay_app%2Fnotify_url.php&sign_type=RSA2&timestamp=2018-01-08+15%3A55%3A57&version=1.0&sign=Ek8r0BA%2FFgo%2B0l8V35C8BoO8vKshaGLhZfNxCvURQBJrIffwHvO0p3JKX3Dx8xnHAuP3opd4JRV%2BlKjd8mH7AICFg44fxlgD2zQvWIzOPfdR%2BH9aJM7SMn9%2FyWP2nVCk2Af4axOGg6llfWFf5vt098lbTZERk6nGjFRv9BuYcNE2hKr4tQFdwlQckGcQqd8uk%2BdWg1pg7j%2FOCZGE7YYmNWj4Zd%2FGm6CWKLTKuIK%2FacL62EoIsRgSDM9KPHnRF1G7QowB5aRcKtQcbDafHmRCI5FcLYUGjcdjMMuDmWKFc4hnXa%2BiEK3biqMMFJE1SvHXqbOOILOz%2FYv%2BUg3HocKM6Q%3D%3D
 
 
 alipay_sdk=alipay-sdk-php-20161101&app_id=2017122601227506&biz_content=%7B%22body%22%3A%22151539383854011000%22%2C%22subject%22%3A%22151539383854011000%22%2C%22out_trade_no%22%3A%22151539383854011000%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A0%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fwww.fb.com%2Fapi%2FCallback%2FaliPayBack&sign_type=RSA2&timestamp=2018-01-08+15%3A51%3A03&version=1.0&sign=bZHDKGBVv8G7KNE8hHKOvnkNmjGRistSeXkM0ArfWRHRJvEw67pqp5z1RpD96A15fYNhnrgna8EKyJ3DpUFKViy8D3q2pF3VA%2FQHOGnUAGk6XnI5%2BY8CYXomKA8GE%2BnFFVPIkxm6aLX3kZcFaNkZYmPNbQoNvCivKL6eaq9LqNXwyWan9iI0xtyW9xr9CN1jXigrTmz03gSUQdisNLvzO1SV7ZRnnY%2Fquvp0yM686X9bKN%2F%2FAV8M8qDf7EKXfKbHGer485Oc5gIxI%2Bku82gR65KVh4j02ROtoQ2N%2FRtw1VPnzc%2BNTAckcho7VAcykLd81mkejtyZP3OZ%2Fov29kk1WQ%3D%3D

 */




- (void)loadCountData{

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"uid"] = myViewModel.user.uid;
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Order/orderState" parameters:parameters completionHandler:^(id responseObject, NSError *error) {

        
        if (!error) {
//                    MYJSONSTR;
            
            
            weakSelf.conutModel = [MyAllOrderCount mj_objectWithKeyValues:responseObject[@"data"]];
            
            
            [self.groupTableView reloadData];

            
            //            NSLog(@"%ld",myViewModel.areas.count);
            
        }
        
        
    }];
    
    
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
